package com.wen.controller;

import com.wen.common.PageDto;
import com.wen.controller.dto.RequestPageDto;
import com.wen.controller.dto.UserDto;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 各种分页演示
 */
@Controller
@RequestMapping("page")
public class PageController {

    @RequestMapping("index")
    public String index() {
        return "page/index";
    }

    @RequestMapping("list")
    public ModelAndView list() {
        ModelAndView mav = new ModelAndView("page/list");

        List<String> content = new ArrayList<String>();
        for (int i = 0; i < 15; i++) {
            content.add(i + "");
        }

        PageDto pageDto = new PageDto();
        pageDto.setTotalRecord(15l);
//        pageDto.setData(content);

        mav.addObject("page", pageDto);

        return mav;
    }

    /**
     * jquery.pager.js分页
     *
     * @return
     */
    @RequestMapping("jPage")
    public ModelAndView jPage(HttpServletRequest request) {
        PageDto pageDto = new PageDto();
        pageDto.setTotalRecord(15l);
        pageDto.setTotalRecord(88l);

        UserDto userDto = new UserDto();
        userDto.setUserName("石头");


        ModelAndView modelAndView = new ModelAndView("page/jPage");
        modelAndView.addObject("pager", pageDto);
        request.setAttribute("userDto", userDto);

        return modelAndView;
    }

    /**
     * jquery.pager.js，内容页
     *
     * @param requestPageDto
     * @return
     */
    @ResponseBody
    @RequestMapping("ajaxPage")
    public ModelAndView ajaxPage(RequestPageDto requestPageDto, HttpServletRequest request) {
        PageDto pageDto = new PageDto();

        UserDto userDto = new UserDto();
        userDto.setUserName("石头1");

        List<String> content = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            content.add(requestPageDto.getPageNumber() + "我的分页");
        }
        pageDto.setResultList(content);

        ModelAndView modelAndView = new ModelAndView("/page/jPageList");
        modelAndView.addObject("data", pageDto);
        modelAndView.addObject("userDto", userDto);
        request.setAttribute("userDto", userDto);

        return modelAndView;
    }
}






